package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * PROJECT_NAME: sky-take-out
 * NAME: ShopingCartServiceImpl
 * USER: zhang
 * DATE: 2023/5/22
 * DESCRIPTION :C端-购物车信息服务层接口实现类
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;

    /**
     * 添加数据购物车
     *
     * @param shoppingCartDTO 要添加到购物车的数据 菜品Id/菜品Id+菜品口味/套餐Id
     */
    @Override
    public void insertShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //将ShoppingCartDTO中的内容拷贝到ShoppingCart中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //通过调用购物车持久层查询购物车信息
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectShoppingCart(shoppingCart);
        if (Objects.nonNull(shoppingCartList) && shoppingCartList.size() == 1) {
            //如果添加的信息存在，对购物车中的数量进行修改更新
            ShoppingCart exitShoppingCart = shoppingCartList.get(0);
            exitShoppingCart.setNumber(exitShoppingCart.getNumber() + 1);
            shoppingCartMapper.updateShoppingCart(exitShoppingCart);
        } else {
            //根据dishId和setmealId判断当前添加的数据是菜品还是套餐
            if (shoppingCart.getDishId() != null) {
                //如果当前DishId不为空说明添加是菜品信息,查询菜品信息
                Dish dish = dishMapper.selectDishById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //如果当前setmealId不为空说明添加是套餐信息,查询套餐信息
                Setmeal setmeal = setmealMapper.selectSetmealById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //将购物车中的剩余内容封装，并添加到购物车中
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insertShoppingCart(shoppingCart);
        }
    }

    /**
     * 查看购物车
     */
    @Override
    public List<ShoppingCart> selectShoppingCart() {
        //将userId封装到购物车对象中
        ShoppingCart conditionShoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        return shoppingCartMapper.selectShoppingCart(conditionShoppingCart);
    }

    /**
     * 删除购物车中的一个商品
     *
     * @param shoppingCartDTO 要删除购物车的数据 菜品Id/菜品Id+菜品口味/套餐Id
     */
    @Override
    public void deleteShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //向数据库查询当前要删除的菜品/套餐
        shoppingCart = shoppingCartMapper.selectShoppingCart(shoppingCart).get(0);
        //如果数量大于1
        if (shoppingCart.getNumber() > 1) {
            // 设置当前商品数量减一，并更新数据库中的数量
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.updateShoppingCart(shoppingCart);
        }else{
            //删除当前的商品
            shoppingCartMapper.deleteShoppingCart(shoppingCart);
        }
    }

    /**
     * 清空购物车功能
     */
    @Override
    public void clearShoppingCart() {
        shoppingCartMapper.clearShoppingCart(BaseContext.getCurrentId());
    }
}
